<template>
  <div v-if="visible" class="numeric-keypad-overlay" @click="handleOverlayClick">
    <div class="numeric-keypad" @click.stop>
      <div class="keypad-header">
        <span class="keypad-title">数字键盘</span>
        <button class="close-btn" @click="close">
          <i class="fas fa-times"></i>
        </button>
      </div>
      <div class="keypad-grid">
        <button 
          v-for="num in 9" 
          :key="num"
          class="keypad-key"
          @click="handleNumberClick(num)"
        >
          {{ num }}
        </button>
        <div class="keypad-spacer"></div>
        <button 
          class="keypad-key"
          @click="handleNumberClick(0)"
        >
          0
        </button>
        <button 
          class="keypad-key delete-btn"
          @click="handleDelete"
        >
          <i class="fas fa-backspace"></i>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits, watch } from 'vue'

const props = defineProps<{
  visible: boolean
  modelValue?: string
}>()

const emit = defineEmits<{
  'update:modelValue': [value: string]
  'close': []
}>()

// 监听 visible 变化，用于调试
watch(() => props.visible, (newVal) => {
  console.log('NumericKeypad visible 变化为:', newVal)
})

const handleNumberClick = (num: number) => {
  const currentValue = props.modelValue || ''
  emit('update:modelValue', currentValue + num.toString())
}

const handleDelete = () => {
  const currentValue = props.modelValue || ''
  if (currentValue.length > 0) {
    emit('update:modelValue', currentValue.slice(0, -1))
  }
}

const close = () => {
  emit('close')
}

const handleOverlayClick = () => {
  close()
}
</script>

<style scoped>
.numeric-keypad-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.numeric-keypad {
  width: 100%;
  max-width: 400px;
  background-color: #2c3e50;
  border-radius: 15px 15px 0 0;
  padding: 15px;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.keypad-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #4a6572;
}

.keypad-title {
  font-size: 1rem;
  color: white;
  font-weight: bold;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 5px 8px;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.keypad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.keypad-spacer {
  /* 占位元素，让 0 按钮居中显示 */
  display: block;
}

.keypad-key {
  aspect-ratio: 1;
  background-color: #4a6572;
  color: white;
  border: none;
  border-radius: var(--border-radius, 8px);
  font-size: 1.5rem;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 45px;
}

.keypad-key:hover {
  background-color: #5d6d7e;
  transform: scale(0.95);
}

.keypad-key:active {
  background-color: #6c7a8a;
  transform: scale(0.9);
}

.delete-btn {
  background-color: #e74c3c;
}

.delete-btn:hover {
  background-color: #c0392b;
}

.delete-btn i {
  font-size: 1.2rem;
}

@media (max-width: 768px) {
  .numeric-keypad {
    max-width: 100%;
  }
  .keypad-key {
    font-size: 1.3rem;
    min-height: 50px;
  }
}
</style>
